﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Case
{
    public partial class AddOrEdit : Form
    {
        
        public AddOrEdit()
        {
            InitializeComponent();
        }
        private string connectionStr = "server=127.0.0.1;port=3306;user=root;password=root;database=studentcase";//连接数据库

        private void textstudentId_TextChanged(object sender, EventArgs e)
        {
            
            //var reader = mySqlCommand.ExecuteReader();
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            string addStr = $"insert into student(studentId,sName,sAge,sLike) values({int.Parse(textstudentId.Text)},'{textname.Text}',{int.Parse(textage.Text)},'{textlike.Text}');";
            MySqlConnection mySqlConnection = new MySqlConnection(connectionStr);


            MySqlCommand mySqlCommand = new MySqlCommand(addStr, mySqlConnection);
            mySqlConnection.Open();
            mySqlCommand.ExecuteNonQuery();
            mySqlCommand.Dispose();
            mySqlConnection.Close();

            textstudentId.Text = "";//清空text内容
            textname.Text = "";
            textage.Text = "";
            textlike.Text = "";
        }
    }
}
